package com.qkst.website.web.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.common.core.BaseController;
import com.qkst.website.common.core.Pager;
import com.qkst.website.web.bean.AccessoryBean;
import com.qkst.website.web.bean.UserBean;
import com.qkst.website.web.service.AccessoryService;

/**
 * @author lihao
 * @description 上传控制器
 * @date 2017-05-11
 * @emial ahlxlihao@163.com
 *
 */
@Controller
@RequestMapping("/web/upload")
public class WebUploadController extends BaseController{

	@Autowired
	private AccessoryService accessoryService;//附件 service
	
	/**
	 * 附件列表页面数据查找
	 * @param request
	 * @param response
	 * @param product
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/accessory/list.html", method = { RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,AccessoryBean accessory) throws IOException{
		ModelAndView mv= this.webModelAndView("accessory/list");
		accessory.setPage(1);
		accessory.setRows(10);
		Pager pager = new Pager();
		//分页查询附件列表
		List<AccessoryBean> accessoryList=accessoryService.queryAccessoryList(accessory);
		//附件总记录数
		int records=accessoryService.queryCount(accessory);
		pager.setRecords(records);
		mv.addObject("accessoryList", accessoryList);//返回附件列表
		mv.addObject("pager", pager);//返回总记录数据
		return mv;
	}
	
	/**添加附件
	 * @param request
	 * @param response
	 * @param collection
	 * @throws IOException
	 */
	@RequestMapping(value = "/accessory/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> add(HttpServletRequest request,HttpServletResponse response,AccessoryBean accessory) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		UserBean user=this.getLoginUser(request, response);
		accessory.setUser(user);//设置附件所属用户信息
		accessory.setUserId(user.getUserId());
		//添加附件
		accessoryService.save(accessory,result);
		return result;
	}
	
	/**删除附件
	 * @param request
	 * @param response
	 * @param accessory
	 * @throws IOException
	 */
	@RequestMapping(value = "/accessory/delete.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> collection_cancel(HttpServletRequest request,HttpServletResponse response,AccessoryBean accessory) throws IOException{
		Map<String, Object> result = new HashMap<String, Object>();
		//删除附件
		accessoryService.delete(accessory,result);
		return result;
	}

}
